<template>
  <div>
    <van-dialog v-if="loading"
                v-model:show="localCtaShow"
                :title="dataInfo.work_qrcode?.guide"
                show-cancel-button
                @close="dialogClose"
                :showConfirmButton="false"
                width="70%"
    >
      <div class="flex justify-center items-center">
        <van-image :src="dataInfo.qrcode_url" fit="cover" />
      </div>
    </van-dialog>
  </div>
</template>

<script setup lang="ts">
import {ref, onMounted, reactive,computed,onUnmounted} from 'vue';
import {ctaCheckPass, ctaInfo, ctaInfoType} from "@/api/cta";

const props = defineProps({
  omniUserUuid: String,
  ctaUuid: {
    type: String,
    required: true
  },
  sceneType: {
    type: String,
    required: true
  },
  sceneUuid: {
    type: String,
    required: true
  },
  ctaShow: {
    type: Boolean,
    default: false
  }
});
const emit = defineEmits(["updateCtaShow", "updateCtaStatus"]);

const localCtaShow = computed({
  get() {
    return props.ctaShow;
  },
  set(value) {
    emit('updateCtaShow', value);
  }
});


const dataInfo = reactive<ctaInfoType>({
  uuid:"",
  mid:0,
  name:"",
  type:"",
  work_qrcode:{
    guide: "",
    reply: "",
    member:""
  },
  wechat_qrcode:{
    guide: "",
    reply: ""
  },
  form: {
    thumb:"",
    material_uuid:"",
    question:[]
  },
  qrcode_url:""
});


let intervalId: ReturnType<typeof setInterval> | undefined;
onMounted(() => {
  console.log("加载CtaWechat")
  getCtaInfo()
  intervalId = setInterval(checkPass, 3000);
});


const loading = ref(false)
const getCtaInfo = async () => {
  try {
    const response = await ctaInfo(props.ctaUuid,props.sceneType,props.sceneUuid);
    if (response.data) {
      Object.assign(dataInfo,response.data)
    }
    loading.value = true
  } catch (e) {
    console.error(e);
  }
}


const dialogClose = () => {
  clearInterval(intervalId);
}


const checkPass = async () => {
  try {
    const response = await ctaCheckPass({
      omni_user_uuid: props.omniUserUuid,
      cta_uuid: props.ctaUuid
    });
    if (response.data?.status) {
      clearInterval(intervalId);
      localCtaShow.value = false;
      emit("updateCtaStatus", true, true);
    }
  } catch (e) {
    console.error(e);
  }
}

onUnmounted(() => {
  console.log("销毁CtaWechat")
  clearInterval(intervalId);
});


</script>

<style scoped>
</style>
